package com.harmonyos.studyharmonyos;

import ohos.aafwk.ability.*;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;

import java.util.Random;

public class TestAbility extends Ability {
    public static final int DEFAULT_DIMENSION_2X2 = 2;
    private static final int INVALID_FORM_ID = -1;
    private static final String EMPTY_STRING = "";
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, TestAbility.class.getName());

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_test);
        Text text = (Text) findComponentById(ResourceTable.Id_tv_text);
        String value = "温度";
        if (intent.getStringParam("params") != null) {
            ZSONObject json = ZSONObject.stringToZSON(intent.getStringParam("params"));
            value = "今日温度：" + json.getString("temperature");
        }
        text.setText(value);
        text.setClickedListener(component -> {
            Intent intent1 = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withBundleName(getBundleName())
                    .withAbilityName("com.harmonyos.studyharmonyos.MainAbility")
                    .build();
            intent1.setOperation(operation);
            startAbility(intent1);
        });
    }

    @Override
    protected ProviderFormInfo onCreateForm(Intent intent) {
        ProviderFormInfo providerFormInfo = new ProviderFormInfo();
        if (intent == null) {
            return providerFormInfo;
        }
        // 获取卡片id
        long formId = INVALID_FORM_ID;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_IDENTITY_KEY)) {
            formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID);
        }
        // 获取卡片名称
        String formName = EMPTY_STRING;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_NAME_KEY)) {
            formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY);
        }
        // 获取卡片规格
        int dimension = DEFAULT_DIMENSION_2X2;
        if (intent.hasParameter(AbilitySlice.PARAM_FORM_DIMENSION_KEY)) {
            dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_2X2);
        }
        FormBindingData formBindingData = new FormBindingData("{\"temperature\": \"点我\"}");
        providerFormInfo.setJsBindingData(formBindingData);
        return providerFormInfo;
    }

    @Override
    protected void onUpdateForm(long formId) {
        super.onUpdateForm(formId);
        ZSONObject zsonObject = new ZSONObject();
        zsonObject.put("temperature", new Random().nextInt(65) + "°");
        FormBindingData formBindingData = new FormBindingData(zsonObject);
        // 调用updateForm接口去更新对应的卡片，仅更新入参中携带的数据信息，其他信息保持不变
        try {
            updateForm(formId, formBindingData);
        } catch (FormException e) {
            e.fillInStackTrace();
        }
    }

    @Override
    protected void onDeleteForm(long formId) {
        HiLog.info(TAG, "onDeleteForm: formId=" + formId);
        super.onDeleteForm(formId);
    }

    @Override
    protected void onTriggerFormEvent(long formId, String message) {
        HiLog.info(TAG, "onTriggerFormEvent: " + message);
        onUpdateForm(formId);
        super.onTriggerFormEvent(formId, message);
    }
}
